package com.mrjoew.weekend.web.shopapi;

import com.alibaba.fastjson.JSONObject;
import com.mrjoew.weekend.web.BaseController;
import com.mrjoew.weekend.entities.vo.shop.CarVO;
import com.mrjoew.weekend.services.shop.CarService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

/**
 * @author Christopher Jones
 * @ClassName CarApiController
 * @explain 购物车
 * @date 2020/11/6 0006 22:07
 */
@Api(tags = "购物车")
@RequestMapping(value = "/shop-api/car")
@RestController
public class CarApiController extends BaseController {

    private final CarService carService;

    public CarApiController(CarService carService) {
        this.carService = carService;
    }

    /**
     * 获取购物车列表
     *
     * @return
     */
    @ApiOperation("购物车列表")
    @GetMapping(value = {"", "/list"})
    public String list(@PageableDefault(size = 20) Pageable pageable) {
        JSONObject result = new JSONObject();
        result.put("state", 1);
        Page<CarVO> voPage = carService.list(pageable, request.getSession().getId());
        result.put("data", voPage);
        return result.toJSONString();
    }

    @ApiOperation("放置购物车")
    @PutMapping(value = "/put/{goodsId}")
    public void put(@PathVariable("goodsId") Integer goodsId, @RequestParam("number") Integer number) {
        carService.put(goodsId, number, request.getSession().getId());
    }

    @ApiOperation("移除购物车")
    @DeleteMapping(value = "/del/{goodsId}")
    public void del(@PathVariable("goodsId") Integer goodsId) {
        carService.del(goodsId, request.getSession().getId());
    }
}
